
package progentiere.knapsackprb;

import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;


public class Item implements Comparable 
{
    
    static int highWeightCoeff = 400;
    static int lowWeightCoeff = 20;
    
    static int itemCounter = 0;
    
    private int id;
    private double weight;
    private double profit;

    public Item() 
    {
        this.id = Item.itemCounter;
        
        Item.itemCounter++;
    }
    
    public Item(double weight, double profit) 
    {
        this();
        this.weight = weight;
        this.profit = profit;
    }
    
    public Item(int id, double weight, double profit) 
    {
        this.id = id;
        this.weight = weight;
        this.profit = profit;
        
        Item.itemCounter++;
    }

    public void generateAtRandom(double capacity, Random generator, double deviation) 
    {        
        this.weight = generator.nextDouble() * (capacity / lowWeightCoeff - capacity / highWeightCoeff) + capacity / highWeightCoeff;
        this.profit = generator.nextDouble() * (2 * deviation * this.weight) + (1 - deviation) * this.weight;
    }
    
    public void readFromFile(Scanner s) 
    {
        this.weight = s.nextDouble();
        this.profit = s.nextDouble();
    }
    
    public void writeInFile(PrintWriter r) 
    {
        r.println(this.toString());
    }
    
    public double getWeight() 
    {
        return weight;
    }

    public double getProfit() 
    {
        return profit;
    }

    @Override
    public String toString() 
    {
        return this.id + " " + this.weight + " " + this.profit;
    }

    @Override
    public boolean equals(Object other)
    {
        Item o = (Item)other;
        if (this.profit == o.profit && this.weight == o.weight)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    @Override
    public int compareTo(Object o) 
    {
        if ((this.profit / this.weight) < ((Item)o).profit / ((Item)o).weight)
        {
            return 1;
        }
        else if ((this.profit / this.weight) > ((Item)o).profit / ((Item)o).weight)
        {
            return -1;
        }
        else 
        {
            return 0;
        }
    }
}
